/**
 * Created by O_o_Vampire on 2016/9/14.
 */
var page = new Object();

/**
 * 此处用于标记页面层次
 * @type {*[]}
 */
page.layer = [
    ["main","found"],//layer1
    [],//layer2
    [] //layer3
];

/**
 * 获取页面层次
 * @returns {string}
 */
page.getLayer = function () {
    var pathName = this.pathName();
    for (i in this.layer) {
        if (-1 < this.layer[i].indexOf(pathName))
            return i;
    }
}

/**
 * 获取页面路径
 * @returns {string}
 */
page.pathName = function () {
    return (window.location.pathname).replace(/.*?html.*?\/(.*?)\.html/, "$1")
}

/**
 * 获取/设置 判断或改变 是否存在存储数据
 * @param b
 */
page.isStore = function (b) {
    if (b) {
        if (b == 1) sessionStorage.setItem("isStore", "1");
        else sessionStorage.setItem("isStore", "0");
        return null;
    }
    if (sessionStorage.getItem("isStore")&&this.getData()){
        return true;
    }else {
        return false
    }
}
/**
 * 清除本地数据
 */
page.cleanItem = function () {
    localStorage.clear();
}

/**
 * 获取本地数据
 */
page.getData = function () {
    return JSON.parse(localStorage.getItem(this.pathName()));
}

/**
 * 储存数据
 * @param data
 */
page.savaData = function (data) {
    if(data == null || "object" != typeof data) {
        console.log("请传入有效的存储数据！");
        return null;
    }
    this.isStore("1");
    localStorage.setItem(this.pathName(),JSON.stringify(data));
}

/**
 * 获取append数据
 * @returns {Array}
 */
page.getAppendData = function () {
    if(localStorage.getItem(this.pathName()+"-append") == null){
        return new Array();
    }
    return JSON.parse(localStorage.getItem(this.pathName()+"-append"));
}

/**
 * 添加本地储存数据
 */
page.appendData = function (data) {

    if(data == null || "object" != typeof data) {
        console.log("请传入有效的存储数据！");
        return null;
    }
    this.isStore("1");
    var array = this.getAppendData();
    array.push(data);
    localStorage.setItem(this.pathName()+"-append",JSON.stringify(array));
}

/**
 * 分支
 */
page.branch = function () {
    if(this.getLayer() == 0){
        if (sessionStorage.getItem("branch") != this.pathName()){
            sessionStorage.setItem("branch",this.pathName());
            this.cleanItem();
        }
    }
}
/**
 * 页面跳转时调用
 */
page.jump = function () {
    if("object" == typeof (this.getData())){
        var obj = this.getData();
        obj.scroll = $(document).scrollTop();
        this.savaData(obj);
    }
}
/**
 * 控制器初始化
 */
page.init = function () {
    if (!this.isStore()) {
        this.cleanItem();
    }
    this.branch();
}
page.init();